<?php

namespace H5\controller;
use Think\Controller;
class LoginController extends Controller {
    
    public function _initialize(){
        S(array('type' => 'File','expire' => '7200'));
        $cfg = C('QYWX');
        $this->appid = $cfg['appid'];
        $this->appsecret = $cfg['appsecret'];
        $this->access_token = S('access_token');
        if(!$this->access_token){
            $this->get_Access_Token();
        } 
    }
    
    
    public function login(){
        $back_url = I('get.backurl');
        if($back_url){
             session('back_url');
        }
        $redirect = urlencode("http://".$_SERVER['SERVER_NAME'] ."/index.php?m=h5&c=login&a=wxcallback");
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$redirect}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
        header('Location: '.$url);       
    }
    
    public function wxcallback(){
        $code = I('get.code');
        if(!$code){
            exit('请求来路不明,程序退出');
        }
        $get_token_url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$this->access_token}&code={$code}";
        //echo $this->access_token;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $get_token_url);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//链接超时时间设置10s
        $res = curl_exec($ch);
        curl_close($ch);
        $json = json_decode($res,true);
        $array = $this->get_user_opid($json);
        $data['openid'] = $array['openid'];
        $this->login_access($data);
    }
    
    
    private function get_Access_Token(){
        $get_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$this->appid}&corpsecret={$this->appsecret}";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $get_token_url);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//链接超时时间设置10s
        $res = curl_exec($ch);
        curl_close($ch);
        $json = json_decode($res,true);
        S('access_token',$json['access_token']);
    }
    
    private function get_user_opid($data = array()){
        $post_data = array('userid' =>$data['UserId']);
        $post_data_p = json_encode($post_data);
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token={$this->access_token}";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
        curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data_p);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_HTTPHEADER,array(
            'Content-type:application/json',
            'Content-Length:'.strlen($post_data_p)));
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//链接超时时间设置10s
        $res = curl_exec($ch);
        curl_close($ch);
        $json = json_decode($res,true);
        return $json;
        //S('access_token',$json['access_token']);
    }
    
    protected function login_access($data){
        //查找用户是否已经在数据库中授权
        $userid = M('user')->where(array('openid' => $data['openid']))->find();
        // print_r($data);
        if(empty($userid)){
            $this->display(':login');
        }else{
            session('userid',$userid['user_id']);
            session('realname',$userid['user_name']);
      		session('level',$userid['oa_level']);
      		if(session('backurl')){
      		    redirect(session('backurl'));
      		}else{
      		    $this->redirect('h5/index/index');
      		}
        }
        
        
    }
} 
